package com.per.yyyFrame.config;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: yyy
 * Date: 2022/2/17
 * Time: 15:09
 * Description:项目加载初始化连接池
 */
@Component
public class DruidInit implements ApplicationListener<ContextRefreshedEvent> {
  public static DataSource mySource;

  @Override
  public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    try {
      Properties pros = new Properties();

      InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("Druid.properties");

      pros.load(is);

      mySource = DruidDataSourceFactory.createDataSource(pros);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public DataSource getSource() throws SQLException {
    return mySource;
  }
}
